// pages/news/news.js
const app = getApp()
var server = require('../../utils/server');
var that,
 x = 0, y = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    btn1Width: 130,
    btn2Width: 130,
    //正在滑动的索引
    scrollIndex: -1,
    //正在删除的索引
    deleteIndex: -1,
    list:[],
    shouquan:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this
  },
  onShow:function(e){
    var userID = app.globalData.userID
    console.log(userID)
    if (userID == ''){
        that.setData({
          shouquan:1
        })
    }else{
      that.conversation()
    }
 
    
  },
  conversation:function(e){
    server.requestForm(false, 'get', 'message/'+app.globalData.userID+'/conversation', {
      // page: ({
      //   size: 4,
      //   index: PageIndex
      // }),
    }, function (e) {
      console.log(e)
      if (e.code == 0) {
        console.log('1111111', e.data)
        for (let i = 0; i < e.data.length; i++) {

          // if (!e.data[i].readState) {
          //   if (e.data[i].lastMessageID == e.data[i].talkerIDArray._id) {
          //     e.data[i].weidu = 1
          //   } else {
          //     e.data[i].weidu = 0
          //   }
          // } else {
          //   e.data[i].weidu = 0
          // }

          console.log('~~~~~~~~~~~~~~~~', e.data[i].readState, e.data[i].lastMessageUserId , app.globalData.userID)
          e.data[i].weidu = !e.data[i].readState && (e.data[i].lastMessageUserId !== app.globalData.userID) 

          var str = e.data[i].createTime
          var d = new Date(str.replace(/-/g, '/'));
          console.log('dddddddd',d)
          if (d.getMinutes() < 10) {
            var Minutes = '0' + d.getMinutes()
          } else {
            var Minutes = d.getMinutes()
          }
          e.data[i].createTime = (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + Minutes;
        }
        console.log('222222', e.data)
        that.setData({
          list: e.data
        })
      } else {
        wx.showToast({
          title: e.message,
          icon: 'none',
          duration: 1000,
        });
      }

    })
  },
  
  newsdetails:function(e){
    console.log(e)
    var name = e.currentTarget.dataset.name;
    var conversationid = e.currentTarget.dataset.conversationid;
    var toheadpicpath = e.currentTarget.dataset.toheadpicpath;
    var toid = e.currentTarget.dataset.toid
    server.requestForm(false, 'put', 'message/modifyUnRead', {
      userID: toid, // app.globalData.userID,
      conversationID: conversationid
    }, function (e) {
      console.log(e)
    })
    wx.navigateTo({
      url: '../newsdetails/newsdetails?name=' + name + '&conversationid=' + conversationid + '&toheadpicpath=' + toheadpicpath + '&toid=' + toid,
    })
  },

  sy: function (e) {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  fx: function (e) {
    wx.reLaunch({
      url: '../find/find',
    })
  },
  sys: function (e) {
    wx.reLaunch({
      url: '../laboratory/laboratory',
    })
  },
  my: function (e) {
    wx.reLaunch({
      url: '../mine/my/my',
    })
  },



	removeMsg: function (e) {
    console.log(e)
    wx.showModal({
      title: '是否删除',
      success: function (res) {
        if (res.confirm) {
          var conversationid = e.currentTarget.dataset.conversationid;
          server.requestForm(false, 'put', 'message/deleteConversation', {
            userID: app.globalData.userID,
            conversationID: conversationid
          }, function (e) {
            that.onShow()
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    var index = e.target.dataset.id;
    //删除确认
    if (this.data.deleteIndex != index) {
      this.setData({ deleteIndex: index });
      setTimeout(function () {
        that.setData({ deleteIndex: -1 });
      }, 2000);
      return false;
    }
    this.data.msg.splice(index, 1);
    this.setData({ scrollIndex: -1 });
    setTimeout(function () {
      that.setData({ msg: that.data.msg, deleteIndex: -1 });
    }, 300);
  },
  touchStart: function (e) {
    x = e.changedTouches[0].clientX;
    y = e.changedTouches[0].clientY;

  },
  touchEnd: function (e) {
    x = x - e.changedTouches[0].clientX;
    y = y - e.changedTouches[0].clientY;
    if (Math.abs(x) < 50) { return; }
    if (Math.abs(x) < Math.abs(y)) { return; }
    var index = e.currentTarget.dataset.id;
    if (index == this.data.scrollIndex) { return; }
    that.setData({ scrollIndex: index });
  },
  // changStatus: function (e) {
  //   var index = e.target.dataset.id;
  //   this.data.msg[index].status = "已读";
  //   this.setData({ msg: this.data.msg });
  // }

})